package com.cxs.studentManagerSystem.service.impl;

import com.cxs.studentManagerSystem.auth.AuthUser;
import com.cxs.studentManagerSystem.base.BasePageBean;
import com.cxs.studentManagerSystem.mapper.RoleMapper;
import com.cxs.studentManagerSystem.mapper.StudentMapper;
import com.cxs.studentManagerSystem.pojo.Role;
import com.cxs.studentManagerSystem.pojo.Student;
import com.cxs.studentManagerSystem.req.user.UserListReq;
import com.cxs.studentManagerSystem.req.user.UserLoginReq;
import com.cxs.studentManagerSystem.service.UserService;
import com.cxs.studentManagerSystem.utils.BeanUtils;
import com.cxs.studentManagerSystem.utils.MyBatisUtil;
import com.cxs.studentManagerSystem.vo.StudentVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Project: studentManagerSystem
 * @Author: cxs2014501@163.com
 * @Create: 2022/9/29 15:12
 * @Description:
 **/
public class UserServiceImpl implements UserService {

    private StudentMapper studentMapper = MyBatisUtil.getMapper(StudentMapper.class);
    private RoleMapper roleMapper = MyBatisUtil.getMapper(RoleMapper.class);

    @Override
    public AuthUser userLogin(UserLoginReq loginReq) {
        List<Student> studentList = studentMapper.selectStudentByUsername(loginReq.getUsername().trim());
        if (studentList == null || studentList.isEmpty()) {
            return null;
        }
        AuthUser authUser = new AuthUser();
        Student student = studentList.get(0);
        BeanUtils.copyOfProperties(student, authUser);
        authUser.setRole(roleMapper.selectRoleById(student.getRoleId()));
        return authUser;
    }

    @Override
    public BasePageBean<StudentVO> userListPage(UserListReq listReq) {
        BasePageBean<StudentVO> result = new BasePageBean<>();
        PageHelper.startPage(listReq.getPageNum(), listReq.getPageSize());
        List<Student> students = studentMapper.selectListByKeyword(listReq.getKeyWord());
        PageInfo<Student> pageInfo = new PageInfo(students);
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setPages(pageInfo.getPages());
        result.setCount(pageInfo.getTotal());
        List<StudentVO> list = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (students != null && students.size() > 0) {
            for (Student student : students) {
                StudentVO vo = new StudentVO();
                BeanUtils.copyOfProperties(student, vo);
                vo.setCreateTime(format.format(student.getCreateTime()));
                vo.setUpdateTime(format.format(student.getUpdateTime()));
                Role role = roleMapper.selectRoleById(student.getRoleId());
                if (null != role) {
                    vo.setRoleName(role.getRoleName());
                }
                list.add(vo);
            }
        }
        result.setData(list);
        return result;
    }

    @Override
    public Integer updateSelectById(Student student) {
        return studentMapper.updateSelectById(student);
    }

    @Override
    public Student getUserInfoById(int id) {
        return studentMapper.selectStudentById(id);
    }
}
